<?php
/**
 * This is the index.ctp
 * @access public
 * @version 1.0
 */
 function formatMailDate($date){
     $year = date('Y',strtotime($date));
     $month = date('m',strtotime($date));
     $day = date('d',strtotime($date));
     $this_year = date('Y');
     $this_month = date('m');
     $this_day = date('d');
     $return = '';
     if(($year == $this_year) && ($month == $this_month) && ($day == $this_day)){
         $return = date('H:i',strtotime($date));
     }
     elseif(($year == $this_year) && ($month == $this_month) && ($day != $this_day)){
         $return = date('M j',strtotime($date));
     }
     elseif(($year == $this_year) && ($month != $this_month) && ($day != $this_day)){
         $return = date('M j',strtotime($date));
     }
     elseif(($year != $this_year) && ($month != $this_month) && ($day != $this_day)){
         $return = date('M j, Y',strtotime($date));
     }
     else{
         $return = $date;
     }

     return $return;
}
?>
<!-- Main content -->
<section role="main" id="main">

    <!-- Header -->
    <?php echo $this->element('page_header'); ?>

    <div class="with-padding">
        <div class="columns">
            <div class="new-row-mobile twelve-columns twelve-columns-tablet twelve-columns-mobile">
                <h3 class="thin underline">Compose and view messages</h3>
                <div class="new-row-mobile twelve-columns twelve-columns-tablet twelve-columns-mobile">

                    <div class="content-panel margin-bottom">

                        <div class="panel-navigation silver-gradient">

                        <div class="panel-control">
                            <a href="<?php echo $this->Html->url(array('controller' => 'Users', 'action' => 'messages')); ?>" class="button blue-gradient icon-undo">Refresh</a>
                        </div>

                        <div class="panel-load-target scrollable" style="height:490px">

                            <div class="navigable">

                                <ul class="unstyled-list open-on-panel-content">
                                    <li class="big-menu grey-gradient with-right-arrow">
                                        <?php
                                        $new_msg_count = 0;
                                        foreach($inbox as $msg){
                                            $msg_reciever = $msg['MessageReciever'];
                                            if($msg_reciever['msg_status'] == 'unread'){
                                                $new_msg_count = $new_msg_count + 1;
                                            }
                                        }
                                        ?>
                                        <span><span class="list-count"><?php echo $new_msg_count;?></span><span class="icon-mail"></span> &nbsp;Inbox</span>
                                        <ul class="message-menu">
                                            <?php
                                            foreach($inbox as $msg){
                                                $msg_sender = $msg['Message'];
                                                $msg_reciever = $msg['MessageReciever'];
                                                $msg_id = $msg_sender['id'];
                                                $reciever_id = $msg_reciever['id'];
                                                ?>
                                                <li>
                                                    <span class="message-status">
                                                      <span class="icon-mail" title="Sent"></span>
                                                        <?php
                                                            if($msg_reciever['msg_status'] == 'unread'){
                                                        ?>
                                                                <a href="javascript:void(0);" class="new-message" title="Unread">New</a>
                                                        <?php
                                                            }
                                                            else{
                                                        ?>
                                                                <span class="old-message"></span>
                                                        <?php
                                                            }
                                                        ?>
                                                    </span>
                                                    <span class="message-info">
                                                        <a href="javascript:void(0);" class="delete" title="Delete" data-id="<?php echo $reciever_id; ?>" delete-type="inbox" delete-action="trash">
                                                            <span class="icon-trash icon-size2 icon-engraving"></span>
                                                        </a>
                                                    </span>
                                                    <a href="javascript:void(0);" class="read" title="Read message" data-id="<?php echo $msg_id; ?>" msg-type="inbox" msg-status="<?php echo $msg_reciever['msg_status']; ?>">
                                                        <span class="blue" style="display: block;"><?php echo formatMailDate($msg_sender['created']); ?></span>

                                                        <strong class="blue"><?php echo $msg_sender['User']['first_name'].' '.$msg_sender['User']['last_name']; ?></strong><br>
                                                        <?php echo $msg_sender['title']; ?>
                                                    </a>
                                                </li>
                                                <?php
                                            }
                                            ?>
                                        </ul>
                                    </li>
                                    <li class="big-menu grey-gradient with-right-arrow">
                                        <span><span class="list-count"><?php echo count($sent);?></span><span class="icon-outbox"></span> &nbsp;Sent</span>
                                        <ul class="message-menu">
                                            <?php
                                                foreach($sent as $msg){
                                                    $msg_sender = $msg['Message'];
                                                    $msg_recievers = $msg['MessageReciever'];
                                                    $msg_id = $msg_sender['id'];
                                            ?>
                                                <li>
                                                    <span class="message-status">
                                                      <span class="icon-outbox" title="Sent"></span>
                                                    <!-- <a href="javascript:void(0);" class="unstarred" title="Not starred"><?php /*echo $msg_sender['title']; */?></a>-->
                                                    </span>
                                                    <span class="message-info">
                                                        <a href="javascript:void(0);" class="delete" title="Delete" data-id="<?php echo $msg_id; ?>" delete-type="sent" delete-action="trash">
                                                            <span class="icon-trash icon-size2 icon-engraving"></span>
                                                        </a>
                                                    </span>
                                                    <a href="javascript:void(0);" class="read" title="Read message" data-id="<?php echo $msg_id; ?>" msg-type="sent" msg-status="read">
                                                        <span class="blue" style="display: block;"><?php echo formatMailDate($msg_sender['created']); ?></span>
                                                        <strong class="blue"><?php echo $msg_recievers[0]['User']['first_name'].' '.$msg_recievers[0]['User']['last_name']; ?></strong><br>
                                                        <?php echo $msg_sender['title']; ?>
                                                    </a>
                                                </li>
                                            <?php
                                                }
                                            ?>
                                        </ul>
                                    </li>
                                    <li class="big-menu grey-gradient with-right-arrow">
                                        <span><span class="list-count"><?php echo count($trash);?></span><span class="icon-trash"></span> &nbsp;Trash</span>
                                        <ul class="message-menu">
                                            <?php
                                            foreach($trash as $msg){
                                                $msg_sender = $msg['Message'];
                                                $msg_recievers = $msg['MessageReciever'];
                                                $msg_id = $msg_sender['id'];
                                                $delete_id = $msg_id; // Defualt to sent
                                                $delete_type = 'sent'; // Defualt to sent
                                                if($msg_sender['type'] == 'inbox'){
                                                    $delete_type = 'inbox';
                                                    $reciever_id = null;
                                                    foreach($msg_recievers as $reciever){
                                                        if($reciever['user_id'] == $authUser['id']){
                                                            $reciever_id = $reciever['id'];
                                                            break;
                                                        }
                                                    }
                                                    $delete_id = $reciever_id;
                                                }
                                                ?>
                                                <li>
                                                    <span class="message-status">
                                                        <?php
                                                        if($msg_sender['type'] == 'inbox'){
                                                            ?>
                                                            <span class="icon-mail" title="Inbox"></span>
                                                            <?php
                                                        }
                                                        else{
                                                            ?>
                                                            <span class="icon-outbox" title="Sent"></span>
                                                            <?php
                                                        }
                                                        ?>
                                                    </span>
                                                    <span class="message-info">
                                                        <a href="javascript:void(0);" class="delete" title="Delete" data-id="<?php echo $delete_id; ?>" delete-type="<?php echo $delete_type; ?>" delete-action="empty_trash">
                                                            <span class="icon-trash icon-size2 icon-engraving"></span>
                                                        </a>
                                                    </span>
                                                    <a href="javascript:void(0);" class="read" title="Read message" data-id="<?php echo $msg_id; ?>" msg-type="trash" msg-status="read">
                                                        <span class="blue" style="display: block;"><?php echo formatMailDate($msg_sender['created']); ?></span>
                                                        <strong class="blue"><?php echo $msg_recievers[0]['User']['first_name'].' '.$msg_recievers[0]['User']['last_name']; ?></strong><br>
                                                        <?php echo $msg_sender['title']; ?>
                                                    </a>
                                                </li>
                                                <?php
                                            }
                                            ?>
                                        </ul>
                                    </li>

                                </ul>

                            </div>

                        </div>

                        </div>

                        <div class="panel-content linen">

                            <div class="panel-control align-right">
                                <a href="javascript:void(0);" class="button blue-gradient icon-star margin-left" id="new_msg">New message</a>
                            </div>

                            <div class="panel-load-target scrollable with-padding" style="height:450px">
                                <div class="msg-window" style="display: none;">
                                    <h2 class="msg-subject thin mid-margin-bottom">Message subject</h2>
                                    <h5 class="msg-from no-margin-top">From: John Doe</h5>
                                    <h5 class="msg-to no-margin-top">To: Bill Doe, Hilarry Homes</h5>

                                    <div class="large-box-shadow white-gradient with-border">

                                        <div class="button-height with-mid-padding silver-gradient no-margin-top">
                                           <!-- <a href="javascript:void(0);" class="button red-gradient float-right with-tooltip" title="Mark as spam">Spam</a>-->
                                            <span class="msg-cotrolls button-group children-tooltip">
                                                <!--<a href="javascript:void(0);" class="button blue-gradient icon-reply" title="Reply to this message">Reply</a>-->
                                               <!-- <a href="javascript:void(0);" class="button" title="Reply to all"><span class="icon-replay-all">Reply to all</span></a>-->
                                                <!--<a href="javascript:void(0);" class="button" title="Forward"><span class="icon-extract">Forward</span></a>-->
                                                <a href="javascript:void(0);" class="button blue-gradient" title="Move to trash"><span class="icon-trash">Delete</span></a>
                                            </span>

                                        </div>

                                        <div class="msg-content with-padding">
                                            <p>Hello,</p>

                                            <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>

                                            <p>Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?</p>

                                            <p>Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>

                                            <p>Best regards,</p>

                                            <p>John</p>
                                        </div>

                                    </div>
                                </div>
                            </div>

                        </div>

                    </div>

                    <div style="display: none;">
                        <div id="mesg-window" style="margin: 20px 5px 5px 5px;">
                            <div class="prompt" ></div>
                            <?php echo $this->Form->create('Message', array('default' => false, 'id' => 'form-new-msg', 'action' => 'add','inputDefaults' => array('label' => false,'div' => false)));?>
                            <fieldset class="fieldset">
                                <legend class="legend">Info</legend>

                                <p class="button-height inline-label">
                                    <label class="label">To:</label>
                                    <?php echo $this->Form->input('MessageReciever.0.user_id', array('id'=>'MessageReciever.0.user_id', 'class' => 'select expandable-list validate[required] ', 'style'=>'width:170px;','options'=>$user_array, 'div' => false, 'label' => false,)); ?>
                                </p>
                                <p class="button-height inline-label">
                                    <label class="label">Title:</label>
                                    <?php echo $this->Form->input('title', array('id'=>'title', 'class' => 'input validate[required] ', 'div' => false, 'label' => false,)); ?>
                                </p>
                                <p class="button-height inline-label">
                                    <label class="label">Body:</label>
                                    <?php echo $this->Form->input('content', array('type'=>'textarea','id'=>'content', 'class' => 'input full-width validate[required]', 'div' => false, 'label' => false,)); ?>
                                    <?php echo $this->Form->input('user_id', array('type'=>'hidden', 'id'=>'user_id', 'class' => '','value'=>$authUser['id'])); ?>
                                </p>

                                <p class="button-height inline-label">
                                    <label for="send-btn" class="label">&nbsp;</label>
                                    <button type="submit" class="button" id="send-btn">
                                        <span class="button-icon green-gradient"><span class="icon-download"></span></span>
                                        Send
                                    </button>

                                    <button type="button" class="button" id="cancel-btn">
                                        <span class="button-icon anthracite-gradient"><span class="icon-cross-round"></span></span>
                                        Cancel
                                    </button>
                                </p>
                            </fieldset>

                            <?php echo $this->Form->end();?>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>

</section>
<!-- End main content -->

<!-- Side tabs shortcuts -->
<?php echo $this->element('side_tabs_shortcuts'); ?>
<!-- Sidebar/drop-down menu -->
<?php echo $this->element('right_side_infos'); ?>

<!-- URL -->
<input type="hidden" id="messages-url" value="<?php echo $this->Html->url(array('controller' => 'Users', 'action' => 'messages')); ?>" />


<?php
echo $this->Html->script('setup.js');

/** Template functions **/
echo $this->Html->script('developr.input.js');
echo $this->Html->script('developr.message.js');
echo $this->Html->script('developr.modal.js');
echo $this->Html->script('developr.navigable.js');
echo $this->Html->script('developr.notify.js');
echo $this->Html->script('developr.scroll.js');
echo $this->Html->script('developr.progress-slider.js');
echo $this->Html->script('developr.tooltip.js');
echo $this->Html->script('developr.confirm.js');
echo $this->Html->script('developr.agenda.js');
echo $this->Html->script('developr.tabs.js');
echo $this->Html->script('libs/glDatePicker/glDatePicker.min3860.js?v=1');

echo $this->Html->script('libs/tinycon.min.js');
echo $this->Html->script('custom-scripts/messages.js');

?>